define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            $(".btn-add").data("area",["1500px","90%"]);
            $(".btn-edit").data("area",["1500px","90%"]);
            Table.api.init({
                extend: {
                    index_url: 'order/allot/index' + location.search,
                    add_url: 'order/allot/add',
                    edit_url: 'order/allot/edit',
/*
                    del_url: 'order/allot/del',
*/
                    multi_url: 'order/allot/multi',
                    import_url: 'order/allot/import',
                    table: 'order_allot',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
/*
                        {field: 'id', title: __('Id')},
*/
                        {field: 'allot_sn', title: __('Allot_sn'), operate: 'LIKE'},
                        {field: 'allottime', title: __('Allottime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
/*
                        {field: 'bin_origin', title: __('Bin_origin'), searchList: {"1":__('Bin_origin 1'),"2":__('Bin_origin 2')}, formatter: Table.api.formatter.normal},
*/
                        {field: 'trafficunit.organ_name', title: __('Traffic_unit'), operate: 'LIKE'},
                        {field: 'allot_number', title: __('Allot_number'), operate: false},
                        {field: 'type.name', title: __('Bin_type'), searchList: $.getJSON('bin.type/searchlist'),},
/*
                        {field: 'bin_native', title: __('Bin_native')},
*/
                        {field: 'takeyard.organ_name', title: __('Take_yard_id'), operate: 'LIKE'},
                        {field: 'take_number', title: __('Take_number'), operate: false},
                        {field: 'admin.nickname', title: __('Salesman_id'), operate: 'LIKE'},
                        {field: 'unit_price', title: __('Unit_price'), operate:'BETWEEN'},
                        {field: 'currency_type', title: __('Currency_type'), searchList: {"1":__('Currency_type 1'),"2":__('Currency_type 2'),"3":__('Currency_type 3'),"4":__('Currency_type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'tenancy', title: __('Tenancy'), operate: false},
                        {field: 'overdue_fee', title: __('Overdue_fee'), operate: false},
                        {
                            field: 'current_status', title: __('调拨状态'), formatter: function (value) {
                                if (value == 1) {
                                    return '出库';
                                } else if (value == 2) {
                                    return '入库';
                                }
                            },
                        },
                        {field: 'aimyard.organ_name', title: __('Aim_yard_id'), operate: 'LIKE'},
                        {field: 'allotouttime', title: __('调出日期'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'allotintime', title: __('调入日期'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'entertyardtime', title: __('进场时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
/*
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
*/
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('调拨入库'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: __('去入库'),
                                    extend:'data-area=\'["1500px","90%"]\' data-shade=\'[0.5,"#000"]\'',
                                    title: __('调拨入库'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'order.allot/to_edit',
                                    hidden:function (row){
                                        if(row.current_status==2){
                                            return true;
                                        }
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('已入库'),
                                    title: __('已入库'),
                                    classname: 'btn btn-xs btn-danger',

                                    hidden: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.current_status==1){
                                            return true;
                                        }

                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },

/*
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
*/
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["1500px","90%"]);
            });
        },
        add: function () {
            $("#c-take_yard_id").focus(function(){
                var bin_type=$("#c-bin_type").val();

                if(!bin_type){

                    Toastr.error('请先选择箱型!');
                    return false;
                }

            });

            $("#c-choose_bin").focus(function(){
                var take_yard_id= $("#c-take_yard_id").val();
                if(!take_yard_id){
                    Toastr.error('请先选择提箱堆场!');
                    return false;
                }

                var aim_yard_id= $("#c-aim_yard_id").val();
                if(!aim_yard_id){
                    Toastr.error('请先选择目的堆场!');
                    return false;
                }

                var take_number= $("#c-take_number").val();
                if(!take_number){
                    Toastr.error('请先填写提箱单号!');
                    return false;
                }
            });

            $("#c-choose_bin").data("eSelect", function(data){
                //后续操作
                var binData=$( "#c-choose_bin").val();
                var bin=  $('#c-choose_bin').selectPageText();

                var bin_type = $("#c-bin_type").val();
                var take_number = $("#c-take_number").val();
                var take_yard_id = $("#c-take_yard_id").val();
                var aim_yard_id = $("#c-aim_yard_id").val();

                $('.btn-remove').trigger("click");


                // 将字符串分割成数组
                var aArray = binData.split(',');
                var bArray = bin.split(',');
                console.log(bArray);


                // 创建一个二维数组，并将aArray和bArray的元素组合起来
                var combined = [];
                for (var i = 0; i < aArray.length; i++) {
                    combined.push([parseInt(aArray[i]), bArray[i]]);
                }
                console.log(combined);

                $('#c-purchase_number').val(aArray.length);



                for (var j = 0; j < bArray.length; j++) {
                    $('.btn-append').trigger("click");
                    $('.bin_id:eq('+j+')').val(aArray[j]);
                    $('.bin_number:eq('+j+')').val(bArray[j]);
                    $('.bin_type').val(bin_type);
                    $('.take_yard_id').val(take_yard_id);
                    $('.aim_yard_id').val(aim_yard_id);
                    $('.take_number').val(take_number);
                    $('.datetimepicker').parent().css('position', 'relative');
                    $('.datetimepicker').datetimepicker({
                        format:'YYYY-MM-DD',
                    });
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        to_edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-bin_type", function(){

                    $('#c-take_yard_id').selectPageClear();
                });

                $(document).on("change", "#c-take_yard_id", function(){

                    $('#c-choose_bin').selectPageClear();
                });

                $('#c-take_yard_id').data("params",function (obj){
                    return {custom: {bin_type: $("#c-bin_type").val(),
                            organ_type:1
                        }};
                });

                $('#c-choose_bin').data("params",function (obj){
                    return {custom: {bin_type: $("#c-bin_type").val(),
                            organ_type:1,
                            take_yard_id:$("#c-take_yard_id").val(),
                        }};
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
